package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {

    /**
     * 数据库连接池
     **/
    public static String driver = "com.mysql.jdbc.Driver";
    public static String url = "jdbc:mysql://localhost/elm admin";
    public static String user = "root";
    public static String password = "123456";

    /**
     * 数据连接的方法
     *
     * @return 连接对象, 如果连接成功返回连接对象否则返回为 NULL
     **/
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            //TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (SQLException e) {
            //TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn,Statement st){
        try{
            if(st != null){
                st.close();
            }
            if (conn!= null){
                conn.close();
            }
        } catch(Exception ex){
            ex.printStackTrace();
        }
    }
    public static void close(Connection conn,Statement st,ResultSet rs){
        try {
            if(st != null){
                st.close();
        }
        if(conn != null){
            conn.close();
        }
        if(rs!=null){
            rs.close();
        }
    } catch(Exception ex){
        ex.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }
}